package net.htjs.juc.completablefuture;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * 多任务组合
 */
@Slf4j
public class Demo3 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 无返回值的任务
        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
            log.info("异步任务执行");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        // 有返回值的任务
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            log.info("异步任务执行");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "hello";
        });
        /*CompletableFuture<Void> all = CompletableFuture.allOf(future1, future2);
        all.thenRun(()->log.info("所有任务完成"));*/
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(future1, future2);
        anyOf.thenRun(()->log.info("有一任务完成"));
        future1.join();
        future2.join();
    }

}
